package com.rayo.keyboxdemo;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.rayo.wallreader.serialport.PortClass;
import com.rayo.wallreader.serialport.ReaderPort;

import java.util.ArrayList;
import java.util.List;

public class ReaderActivity extends AppCompatActivity implements View.OnClickListener, IReaderView {
    private Button mBtnPort, mBtnFindKey, mBtnSettingKey, mBtnEmerKey, mBtnEventKey,
            mBtnBlackListKey, mBtnSystemKey, mBtnReadLockIdKey,
            mBtnReadLockSerialKey, mBtnEngKey, mBtnUserKey, mBtnReadKey,
            mResetKey, mBeep, mCancel, mRelay, mBtnKeyCanOpen;

    private TextView mTvMsg;

    private ImageView mIvLoad, mIvResult;
    private Animation mAnimLoad;

    private ReaderPresenter presenter;

    private int yourChoice = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reader);
        presenter = new ReaderPresenter(this);
        initView();
    }

    private void initView() {
        mBtnPort = (Button) findViewById(R.id.btn_port);
        mBtnFindKey = (Button) findViewById(R.id.btn_findKey);
        mBtnSettingKey = (Button) findViewById(R.id.btn_SettingKey);
        mBtnEmerKey = (Button) findViewById(R.id.btn_EmergKey);
        mBtnEventKey = (Button) findViewById(R.id.btn_EventKey);
        mBtnBlackListKey = (Button) findViewById(R.id.btn_BlackListKey);
        mBtnSystemKey = (Button) findViewById(R.id.btn_SystemCodeKey);
        mBtnReadLockIdKey = (Button) findViewById(R.id.btn_ReadLockIdKey);
        mBtnReadLockSerialKey = (Button) findViewById(R.id.btn_ReadSerialKey);
        mBtnEngKey = (Button) findViewById(R.id.btn_EngKey);
        mBtnUserKey = (Button) findViewById(R.id.btn_UserKey);
        mBtnReadKey = (Button) findViewById(R.id.btn_ReadKey);
        mResetKey = (Button) findViewById(R.id.btn_ResetKey);
        mBeep = (Button) findViewById(R.id.btn_Beep);
        mCancel = (Button) findViewById(R.id.btn_Cancel);
        mRelay = (Button) findViewById(R.id.btn_Relay);
        mBtnPort.setOnClickListener(this);
        mBtnFindKey.setOnClickListener(this);
        mBtnSettingKey.setOnClickListener(this);
        mBtnEmerKey.setOnClickListener(this);
        mBtnEventKey.setOnClickListener(this);
        mBtnBlackListKey.setOnClickListener(this);
        mBtnSystemKey.setOnClickListener(this);
        mBtnReadLockIdKey.setOnClickListener(this);
        mBtnReadLockSerialKey.setOnClickListener(this);
        mBtnEngKey.setOnClickListener(this);
        mBtnUserKey.setOnClickListener(this);
        mBtnReadKey.setOnClickListener(this);
        mResetKey.setOnClickListener(this);
        mBeep.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        mRelay.setOnClickListener(this);

        mBtnKeyCanOpen = (Button) findViewById(R.id.btn_KeyCanOpen);
        mBtnKeyCanOpen.setOnClickListener(this);
        mTvMsg = (TextView) findViewById(R.id.tv_msg);
        mTvMsg.setText("");

        mIvResult = (ImageView) findViewById(R.id.iv_result);
        mIvLoad = (ImageView) findViewById(R.id.iv_loading);

        mAnimLoad = AnimationUtils.loadAnimation(this, R.anim.loading);
        LinearInterpolator lin = new LinearInterpolator();
        mAnimLoad.setInterpolator(lin);
        mBtnPort.setText("TTYS0");
        List<PortClass> port = new ArrayList<>();
        PortClass portClass = new PortClass();
        portClass.setPort(PortClass.DETIVER_0);
        port.add(portClass);
        presenter.setPort(port);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @Override
    public void showLoading(boolean isShow) {
        if (isShow) {
            mIvLoad.setVisibility(View.VISIBLE);
            if (mAnimLoad != null && mIvLoad != null) {
                mIvLoad.clearAnimation();
                mIvLoad.startAnimation(mAnimLoad);
            }
        } else {
            if (mAnimLoad != null && mIvLoad != null) {
                mIvLoad.clearAnimation();
            }
            if (mIvLoad != null) {
                mIvLoad.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void showResultImage(boolean isSuccess) {
        if (null != mIvResult) {
            if (isSuccess) {
                mIvResult.setImageResource(R.drawable.success);
            } else {
                mIvResult.setImageResource(R.drawable.failed);
            }
            mIvResult.setVisibility(View.VISIBLE);
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1500);
                        mIvResult.setVisibility(View.GONE);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void setMsg(String txt) {
        mTvMsg.setText(mTvMsg.getText() + "\n" + txt);
    }

    @Override
    public void cleanMsg() {
        mTvMsg.setText("");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_port:
                final View portDialogView = LayoutInflater.from(this)
                        .inflate(R.layout.dialog_port, null);
                AlertDialog.Builder portDialog = new AlertDialog.Builder(this);
                RadioGroup rgPort = portDialogView.findViewById(R.id.rg_port);
                final RadioButton rbTtys0 = portDialogView.findViewById(R.id.rb_ttys0);
                final RadioButton rbTtys1 = portDialogView.findViewById(R.id.rb_ttys1);
                final RadioButton rbTtys2 = portDialogView.findViewById(R.id.rb_ttys2);

                final CheckBox checkBoxReader1 = portDialogView.findViewById(R.id.checkBox_plugIn1);
                final CheckBox checkBoxReader2 = portDialogView.findViewById(R.id.checkBox_plugIn2);
                final CheckBox checkBoxReader3 = portDialogView.findViewById(R.id.checkBox_plugIn3);
                final List<PortClass> port = new ArrayList<>();
                final PortClass portClass = new PortClass();

                rgPort.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, int i) {
                        if (rbTtys0.isChecked()) {
                            yourChoice = 0;
                        } else if (rbTtys1.isChecked()) {
                            yourChoice = 1;
                        } else if (rbTtys2.isChecked()) {
                            yourChoice = 2;
                        } else {

                        }
                    }
                });

                portDialog.setView(portDialogView);
                portDialog.setTitle("Set Port");
                portDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                portDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (yourChoice) {
                            case 0:
                                mBtnPort.setText("TTYS0");
                                portClass.setPort(ReaderPort.DETIVER_0);
                                break;
                            case 1:
                                mBtnPort.setText("TTYS1");
                                portClass.setPort(ReaderPort.DETIVER_1);
                                break;
                            case 2:
                                mBtnPort.setText("TTYS2");
                                portClass.setPort(ReaderPort.DETIVER_2);
                                break;
                            default:
                                break;
                        }
                        portClass.setBleKey(checkBoxReader1.isChecked());
                        portClass.setNfcKey(checkBoxReader2.isChecked());
                        portClass.setCard(checkBoxReader3.isChecked());
                        port.add(portClass);
                        presenter.setPort(port);
                    }
                });
                portDialog.show();
                break;
            case R.id.btn_findKey:
                presenter.setReader(1);
                break;
            case R.id.btn_SettingKey:
                presenter.setReader(2);
                break;
            case R.id.btn_EmergKey:
                presenter.setReader(3);
                break;
            case R.id.btn_EventKey:
                presenter.setReader(4);
                break;
            case R.id.btn_EngKey:
                presenter.setReader(5);
                break;
            case R.id.btn_BlackListKey:
                presenter.setReader(6);
                break;
            case R.id.btn_SystemCodeKey:
                presenter.setReader(7);
                break;
            case R.id.btn_ReadLockIdKey:
                presenter.setReader(8);
                break;
            case R.id.btn_ReadSerialKey:
                presenter.setReader(9);
                break;
            case R.id.btn_UserKey:
                presenter.setReader(10);
                break;
            case R.id.btn_ReadKey:
                presenter.setReader(11);
                break;
            case R.id.btn_ResetKey:
                presenter.setReader(12);
                break;
            case R.id.btn_Beep:
                presenter.setReader(13);
                break;
            case R.id.btn_Cancel:
                presenter.setReader(14);
                break;
            case R.id.btn_Relay:
                AlertDialog.Builder relayDialog = new AlertDialog.Builder(this);
                List<String> relays = new ArrayList<>();
                relays.add("HOME RELAY");
                int plugInReader[] = presenter.getPlugInReader();
                for (int i : plugInReader) {
                    switch (i) {
                        case 1:
                            relays.add("PlugIn Reader1");
                            break;
                        case 2:
                            relays.add("PlugIn Reader2");
                            break;
                        case 3:
                            relays.add("PlugIn Reader3");
                            break;
                        case 4:
                            relays.add("PlugIn Reader4");
                            break;
                        default:
                            break;
                    }
                }
                final String[] relay = new String[relays.size()];
                for (int i = 0; i < relays.size(); i++) {
                    relay[i] = relays.get(i);
                }
                relayDialog.setTitle("Open Relay");
                relayDialog.setSingleChoiceItems(relay, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
                relayDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                relayDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (relay[yourChoice]) {
                            case "HOME RELAY":
                                presenter.openSec();
                                break;
                            case "PlugIn Reader1":
                                presenter.openPlugInRelay(1);
                                break;
                            case "PlugIn Reader2":
                                presenter.openPlugInRelay(2);
                                break;
                            case "PlugIn Reader3":
                                presenter.openPlugInRelay(3);
                                break;
                            case "PlugIn Reader4":
                                presenter.openPlugInRelay(4);
                                break;
                            default:
                                break;
                        }
                    }
                });
                relayDialog.show();
                break;
            case R.id.btn_KeyCanOpen:
                presenter.setReader(15);
                break;
            default:
                break;
        }
    }
}
